package com.xyazm.order.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.xyazm.common.constants.RocketMqConstant;
import com.xyazm.common.enums.OrderStatusEnum;
import com.xyazm.coupon.domain.param.ReleaseUserCouponParam;
import com.xyazm.inventory.domain.param.ReleaseProductStockParam;
import com.xyazm.order.dao.OrderItemDAO;
import com.xyazm.order.domain.dto.OrderInfoDTO;
import com.xyazm.order.domain.entity.OrderItemDO;
import com.xyazm.order.domain.param.CancelOrderAssembleParam;
import com.xyazm.order.mapstruct.OrderApiMapstruct;
import com.xyazm.order.service.IAfterSaleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 监听 释放资产消息 2.0
 * @Author: xuyou
 * @Date: 2022/6/30 16:23
 */
@Slf4j
@Component
@RocketMQMessageListener(
        topic = RocketMqConstant.RELEASE_ASSETS_TOPIC,
        consumerGroup = RocketMqConstant.CANCEL_REFUND_CONSUMER_GROUP,
        consumeMode = ConsumeMode.CONCURRENTLY,
        messageModel = MessageModel.CLUSTERING,
        consumeThreadMax = 1
)
public class ReleaseAssetsListener extends AbstractRocketMqListener {

    @Autowired
    private OrderItemDAO orderItemDAO;
    @Autowired
    private IAfterSaleService afterSaleService;


    @Override
    public void onMessage(String message) {
        log.info("ReleaseAssetsListener message:{}", message);
        CancelOrderAssembleParam cancelOrderAssembleParam = JSONObject.parseObject(message, CancelOrderAssembleParam.class);
        // 未支付的订单不需要进入取消退款流程,未付款不记售后单
        OrderInfoDTO orderInfoDTO = cancelOrderAssembleParam.getOrderInfoDTO();
        if (orderInfoDTO.getOrderStatus() <= OrderStatusEnum.CREATED.getCode()) {
            return;
        }
        // 执行取消订单前操作
        afterSaleService.processCancelOrder(cancelOrderAssembleParam);
//
//
//                String message = new String(messageExt.getBody());
//                log.info("ReleaseAssetsConsumer message:{}", message);
//                CancelOrderAssembleDTO cancelOrderAssembleDTO = JSONObject.parseObject(message,
//                        CancelOrderAssembleDTO.class);
//
//                // 1.发送取消订单退款请求MQ
//                OrderInfoDTO orderInfoDTO = cancelOrderAssembleDTO.getOrderInfoDTO();
//                // 订单已支付,执行退款逻辑
//                if (orderInfoDTO.getOrderStatus() > OrderStatusEnum.CREATED.getCode()) {
//                    defaultProducer.sendMessage(RocketMqTopicEnum.CANCEL_REFUND.getTopic(),
//                            JSONObject.toJSONString(cancelOrderAssembleDTO), "取消订单退款", null, orderInfoDTO.getOrderId());
//                }
//
//                // 2.释放商品库存
//                ReleaseProductStockParam releaseProductStockParam = buildReleaseProductStockParam(orderInfoDTO);
//                defaultProducer.sendMessage(RocketMqTopicEnum.RELEASE_INVENTORY.getTopic(),
//                        JSONObject.toJSONString(releaseProductStockParam), "取消订单释放库存", null, orderInfoDTO.getOrderId());
//
//                // 3.释放优惠券,只有未支付就取消的时候才可以直接退优惠券
//                if (StringUtils.isNotBlank(orderInfoDTO.getCouponId()) &&
//                        orderInfoDTO.getOrderStatus().equals(OrderStatusEnum.CREATED.getCode())) {
//                    ReleaseUserCouponParam releaseUserCouponParam = buildReleaseUserCouponParam(orderInfoDTO);
//                    defaultProducer.sendMessage(RocketMqTopicEnum.RELEASE_COUPON.getTopic(),
//                            JSONObject.toJSONString(releaseUserCouponParam), "取消订单释放优惠券", null, orderInfoDTO.getOrderId());
//                }
    }

    /**
     * 构建 取消订单释放商品库存 对象
     * @author xuyou
     * @date: 2022/6/30 16:52
     * @param: orderInfoDTO
     */
    private ReleaseProductStockParam buildReleaseProductStockParam(OrderInfoDTO orderInfoDTO) {
        // 根据订单id查询订单明细数据
        List<OrderItemDO> orderItemDOList = orderItemDAO.listByOrderId(orderInfoDTO.getOrderId());
        List<ReleaseProductStockParam.OrderItemParam> orderItemParamList = OrderApiMapstruct.INSTANCE.convertOrderItemParam(orderItemDOList);
        ReleaseProductStockParam releaseProductStockParam = new ReleaseProductStockParam();
        releaseProductStockParam.setOrderId(orderInfoDTO.getOrderId());
        releaseProductStockParam.setOrderItemParamList(orderItemParamList);
        return releaseProductStockParam;
    }


    private ReleaseUserCouponParam buildReleaseUserCouponParam(OrderInfoDTO orderInfoDTO) {
        ReleaseUserCouponParam releaseUserCouponParam = new ReleaseUserCouponParam();
        releaseUserCouponParam.setOrderId(orderInfoDTO.getOrderId());
        releaseUserCouponParam.setCouponId(orderInfoDTO.getCouponId());
        releaseUserCouponParam.setUserId(orderInfoDTO.getUserId());
        return releaseUserCouponParam;
    }
}
